package com.project.bookstore.exception.handler;

import com.project.bookstore.common.CommonResult;
import com.project.bookstore.common.State;
import com.project.bookstore.exception.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    //处理 service 异常
    @ExceptionHandler(ServiceException.class)
    public CommonResult<Void> serviceExceptionHandler(ServiceException e){
        return CommonResult.fail(e);
    }
    //处理其他异常
    @ExceptionHandler(Throwable.class)
    public CommonResult<Void> otherExceptionHandler(Throwable e){
        e.printStackTrace();
        return CommonResult.fail(State.INTERNAL_EXCEPTION, e.getMessage());
    }
}
